from rest_framework import status
from rest_framework.permissions import IsAuthenticated
from rest_framework.views import APIView
from rest_framework.response import Response
from users.models import User
from rest_framework.generics import CreateAPIView, RetrieveAPIView

# Create your views here.

# 用户注册验证
from users.serializers import RegisterCreateSerializer, UserDetailSerializer


class RegisterCreateView(CreateAPIView):
    """

    POST        /users/
    """
    serializer_class = RegisterCreateSerializer


# 短信验证
class RegisterPhoneCountAPIView(APIView):
    '''
    # GET           /users/phones/(?P<mobile>1[345789]\d{9})/count/
    # request       query_params
                    mobile	    str	手机号
    # response      data
    #               mobile	    str	手机号
    #               count	    int	数量

    '''

    def get(self, request, mobile):
        count = User.objects.filter(mobile=mobile).count()
        context = {
            'count': count,
            'mobile': mobile
        }
        return Response(context)


# 用户名验证
class RegisterUsernameCountAPIView(APIView):
    '''
    # GET           /users/usernames/(?P<username>\w{5,20})/count/
    # request       query_params
                    username	str	用户名
    # response      data
    #               username	str	用户名
    #               count       int 数量
    '''

    def get(self, request, username):
        count = User.objects.filter(username=username).count()
        context = {
            'count': count,
            'username': username
        }
        return Response(context)


class UserDetailView(RetrieveAPIView):
    """
    获取登录用户的信息
    GET /users/infos/
    既然是登录用户,我们就要用到权限管理
    在类视图对象中也保存了请求对象request
    request对象的user属性是通过认证检验之后的请求用户对象
    """
    permission_classes = [IsAuthenticated]

    serializer_class = UserDetailSerializer

    def get_object(self):
        return self.request.user

    pass


from rest_framework.permissions import IsAuthenticated
from rest_framework.generics import UpdateAPIView
from .serializers import EmailSerializer


class EmailView(UpdateAPIView):
    """
    保存邮箱
    PUT /users/emails/
    """

    permission_classes = [IsAuthenticated]

    serializer_class = EmailSerializer

    def get_object(self):
        return self.request.user


class VerificationEmailView(APIView):
    """
    验证激活邮箱
    GET /users/emails/verification/?token=xxxx

    思路:
    获取token,并判断
    获取 token中的id
    查询用户,并判断是否存在
    修改状态
    返回响应
    """

    def get(self,request):
        token = request.query_params.get('token')
        if not token:
            return Response({'message':'缺少token'},status=status.HTTP_400_BAD_REQUEST)
        # 获取token中的id,email
        # 查询用户, 并判断是否存在
        user = User.check_verify_email_token(token)
        if user is None:
            return Response({'message':'链接无效'},status=status.HTTP_400_BAD_REQUEST)
        else:
            # 修改状态
            user.email_active = True
            user.save()
            # 返回响应
            return Response({'message':'ok'})
